<?php
namespace app\index\controller;
use app\model\Category as CategoryModel;
use think\Db;


class Index extends Base
{
    public $exception = ['index'];
    
    public function __construct(){
        $model = new CategoryModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '首页';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function contact(){
        return view('contact', [
      
        ]);
    }

    public function index(){
        $banners = Db::name('banner')->where([
            ['status','=',1]
        ])->order('sort','desc')->select();
        foreach ($banners as $k => $vo) {
            // $banners[$k]['translate_x'] = ($k-1) * 1910;
            $banners[$k]['translate_x'] = 0;
        }


        $products = Db::name('product')->where([
            ['is_home','=',1]
        ])->order('sort','desc')->limit(15)->select();

        return view('index', [
            'banners'=>$banners,
            'products'=>$products
        ]);
    }

    public function activity(){
        $activitys = Db::name('activity')->field('content',true)->limit(4)->select();
        return view('activity', [
            'activitys'=>$activitys,
            'banners'=>1
        ]);
    }

    public function search(){
        $q = input('q');
        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',12)
        ];
        $list = Db::name('search_view')->where($map)->where('title|description','like','%'.$q.'%')->paginate($page_param);
        $page = $list->render();
        $page = indexPage($page);

        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($q)) {
                $list['data'][$k]['title'] = str_replace($q,'<strong>'.$q.'</strong>',$vo['title']);
                $list['data'][$k]['description'] = str_replace($q,'<strong>'.$q.'</strong>',$vo['description']);
            }

            if ($vo['type'] == 1) {
                $list['data'][$k]['category_name'] = Db::name('category')->where('id',$vo['category_id'])->value('name');
                $list['data'][$k]['category_url'] = url('category/index',['id'=>$ov['category_id']]);
                $list['data'][$k]['url'] = url('product/detail',['id'=>$vo['id']]);
            }elseif ($vo['type'] == 2) {
                $list['data'][$k]['category_name'] = '最新咨询';
                $list['data'][$k]['category_url'] = url('article/index');
                $list['data'][$k]['url'] = url('article/detail',['id'=>$vo['id']]);
            }elseif ($vo['type'] == 3) {
                $list['data'][$k]['category_name'] = '最新活动';
                $list['data'][$k]['category_url'] = url('index/activity');
                $list['data'][$k]['url'] = url('activity/detail',['id'=>$vo['id']]);
            }
        }

        return view('search', [
            'q'=>$q,
            'list'=>$list,
            'page'=>$page
        ]);
    }

    
}
